import { Component, OnInit, Injector } from '@angular/core';
import { AppComponentBase } from '@shared/common/app-component-base';
import { ServiceProxy, DictionaryDTO } from '@shared/service-proxy/service-proxies';
import { FormBuilder, Validators } from '@angular/forms';

@Component({
  selector: 'app-site-type-config',
  templateUrl: './site-type-config.component.html',
  styleUrls: ['./site-type-config.component.less']
})
export class SiteTypeConfigComponent extends AppComponentBase implements OnInit {
  dataSet = [];
  loading = true;
  isVisible = false;
  isOkLoading = false;
  dic: DictionaryDTO = new DictionaryDTO();
  title: string;
  validateForm;
  constructor(injector: Injector,
    private fb: FormBuilder,
    private _service: ServiceProxy) {
    super(injector)
  }

  ngOnInit() {
    this.validateForm = this.fb.group({
      itemValue: [null, [Validators.required, Validators.maxLength(255)]],
      itemKey: [null, [Validators.required, Validators.maxLength(255)]],
    });
    debugger;
    this.load();
  }

  load(): void {
    this.loading = true;
    this._service.getDictionaryByDictionaryTypeUsingGET('site_type').subscribe(result => {
      this.dataSet = result.result.fieldList;
      this.loading = false;
    })
  }

  showModal(id?: string) {
    this.dic = new DictionaryDTO();
    if (id) {
      var data = this.dataSet.find(m => m.id == id);
      this.dic.dictionaryName = data.dictionaryName;
      this.dic.dictionaryType = data.dictionaryType;
      this.dic.itemKey = data.itemKey;
      this.dic.itemValue = data.itemValue;
      this.dic.id = data.id;

      this.title = this.l("edit");
    }
    else {
      this.validateForm.reset();
      this.title = this.l("create");
    }
    this.isVisible = true;
  }

  handleOk(): void {
    this.submitForm();
  }

  handleCancel(): void {
    this.isVisible = false;
  }

  submitForm(): void {
    this.isOkLoading = true;
    for (const i in this.validateForm.controls) {
      this.validateForm.controls[i].markAsDirty();
      this.validateForm.controls[i].updateValueAndValidity();
    }
    this.dic.dictionaryName = "站点类别字典";
    this.dic.dictionaryType = "site_type";
    if (this.validateForm.valid) { // 验证通过
      if (this.dic.id) {
        this._service.updateDictionaryUsingPUT(this.dic.id, this.dic).subscribe(result => {
          this.isOkLoading = false;
          if (result.success) {
            this.load();
            this.isVisible = false;
            this.message.success(this.l("save_success"));
          }
          else {
            this.message.info(this.l(result.error.message));
          }

        }, error => {
          this.isOkLoading = false;
          this.message.error(this.l(error.message));
        })
      }
      else {
        this._service.createDictionaryUsingPOST(this.dic).subscribe(result => {
          this.isOkLoading = false;
          if (result.success) {
            this.load();
            this.isVisible = false;
            this.message.success(this.l("save_success"));
          }
          else
            this.message.info(this.l(result.error.message));
        }, error => {
          this.isOkLoading = false;
          this.message.error(this.l(error.message));
        })
      }

    } else { // 验证未通过
      this.isOkLoading = false;
      this.message.error(this.l('verify_failed_please_check_the_form_item'));
    }

  }
}
